// pages/realNameAuthent/realNameAuthent.js
var that,
  app = getApp(),
  api = new (require('../../utils/api.js')),
  WxParse = require('../../wxParse/wxParse.js'),
  util = new (require('../../utils/util.js')),
  headimg = api.getimgurl();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    realname: '',
    idcard: '',
    zidcardimg: '',
    fidcardimg: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
  },
  inputtap(e) {
    let { prop } = e.currentTarget.dataset
    that.setData({ [prop]: e.detail.value })
  },
  addimg(e) {
    let { prop } = e.currentTarget.dataset
    if (prop == 'z') {
      wx.chooseImage({
        count: 1,
        success(res) {
          that.setData({ zidcardimg: res.tempFilePaths[0] })
        }
      })
    } else {
      wx.chooseImage({
        count: 1,
        success(res) {
          that.setData({ fidcardimg: res.tempFilePaths[0] })
        }
      })
    }
  },
  authentication() {
    let { zidcardimg, fidcardimg, realname, idcard } = that.data, err = '', imglist = []
    if(fidcardimg=='') err='请添加身份证照片（国徽面）'
    if(zidcardimg=='') err='请添加身份证照片（头像面）'
    if(realname=='') err='请输入真实姓名'
    if(idcard=='') err='请输入身份证号'
    if(err.length>0){
      wx.showToast({
        title: err,
        icon:'none'
      })
      return
    }
    wx.showLoading({
      title:'提交中...'
    })
    imglist.push(zidcardimg,fidcardimg)
    api.uploadimg({
      url: 'https://techcherry.cn/jzgapi/upload/wx_upload', //这里是你图片上传的接口
      path: imglist //这里是选取的图片的地址数组        
    }, {
      success: function (res) {
        wx.hideLoading()
        if (res.length == imglist.length) {
        let idphoto = res.toString()
          api.addRealnameApply({idphoto,realname,idcard}, res => {
            wx.hideLoading()
            if (res.data.issuccess == 1) {
              wx.showModal({
                title: '提示',
                content: '提交成功',
                showCancel: false,
                success(res) {
                  if (res.confirm) {
                    wx.navigateBack()
                  }
                }
              })
            } else {
              wx.showModal({
                title: '提示',
                content:res.data.msg,
                showCancel: false,
                success(res1) {
                  if (res1.confirm) {
                    // wx.navigateBack()
                  }
                }
              })
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '图片上传失败',
            showCancel: false,
            success(res1) {
              if (res1.confirm) {
                // wx.navigateBack()
              }
            }
          })
        }
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})